/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.UptimeInterface;
import br.gov.cemaden.library.model.smp.ExecHardware;
import br.gov.cemaden.library.model.smp.Uptime;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class UptimeDao extends GenericDao implements UptimeInterface {

    private Session session;

    public UptimeDao(Session session) {
        this.session = session;
    }

    public List<Uptime> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Uptime> uptime = null;
        try {
            Query query = session.getNamedQuery("Uptime.findAll");
            uptime = (List<Uptime>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no UptimeFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return uptime;
    }

    public Uptime findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Uptime uptime = null;
        try {
            Query query = session.getNamedQuery("Uptime.findById").setParameter("id", id);
            uptime = (Uptime) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no UptimeFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return uptime;
    }

    public Uptime findByUptime(String uptime) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Uptime up = null;
        try {
            Query query = session.getNamedQuery("Uptime.findByUptime").setParameter("uptime", uptime);
            up = (Uptime) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no UptimeFacade.findByUptime: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return up;
    }

    public List<Uptime> findByExecHardware(ExecHardware execHardware) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Uptime> uptime = null;
        try {
            Query query = session.getNamedQuery("Uptime.findByExecHardware").setParameter("execHardware", execHardware);
            uptime = (List<Uptime>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no UptimeFacade.findByExecHardware: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return uptime;
    }

    public Uptime insert(Uptime uptime) {
        try {
            saveOrUpdate(this.session, uptime);
        } catch (Exception ex) {
            Logger.getLogger(UptimeDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return uptime;
    }

    public Uptime update(Uptime uptime) {
        try {
            saveOrUpdate(this.session, uptime);
        } catch (Exception ex) {
            Logger.getLogger(UptimeDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return uptime;
    }

    public void delete(Uptime uptime) {
        try {
            remove(this.session, uptime);
        } catch (Exception ex) {
            Logger.getLogger(UptimeDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
